from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QGridLayout, QLabel, QSpinBox, QMessageBox, QWidget, QPushButton

from reboot_pc import reboot_pc


class ShutdownPause(QWidget):

    def change_time_to_shutdown(self):
        filename = '/usr/share/plasma/look-and-feel/org.kde.breeze.desktop/contents/logout/Logout.qml'
        with open(filename, 'r') as inp:
            lines = inp.readlines()
        for i in range(len(lines)):
            if 'property real timeout' in lines[i]:
                lines[i] = lines[i].split(':')[0] + f': {self.spinBox.value()}'
            else:
                lines[i] = lines[i].rstrip()
        with open(filename, 'w') as out:
            print(*lines, sep='\n', file=out)
        fin = ''
        if self.spinBox.value() in (1, 21, 31, 41, 51):
            fin = 'у'
        elif self.spinBox.value() in (2, 3, 4) or self.spinBox.value() > 20 and \
            self.spinBox.value() % 10 in (2, 3, 4):
            fin = 'ы'
        dlg = QMessageBox()
        dlg.setWindowTitle('Внимание!')
        dlg.setText(f'Установлена пауза в {self.spinBox.value()} секунд{fin} при выключении компьютера.')
        dlg.exec()

    def __init__(self):
        super().__init__()

        self.setWindowTitle("Изменение паузы при выключении")

        # QBtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
        #
        # self.buttonBox = QDialogButtonBox(QBtn)
        # self.buttonBox.accepted.connect(self.change_time_to_shutdown)
        # self.buttonBox.rejected.connect(self.close)
        layout = QGridLayout()


        shutdown_header_label = QLabel("Выключение и перезагрузка")
        shutdown_header_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(shutdown_header_label, 0, 0, 1, 2)
        info_label = QLabel("Пауза в секундах при выключении ПК: ")
        info_label.setAlignment(Qt.AlignRight)
        info_label.setWordWrap(True)
        layout.addWidget(info_label, 1, 0)
        self.spinBox = QSpinBox(self)
        self.spinBox.setRange(1, 60)
        layout.addWidget(self.spinBox, 1, 1)

        reboot_button = QPushButton('Перезагрузить')
        reboot_button.clicked.connect(reboot_pc)
        layout.addWidget(reboot_button, 2, 1)

        self.setLayout(layout)
        # self.show()
        layout.addWidget(QLabel(''), 3, 0)
        layout.setRowStretch(0, 0)
        layout.setRowStretch(1, 0)
        layout.setRowStretch(2, 0)
        layout.setRowStretch(3, 1)
